/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaiementDialog.java
 *
 * Created on 10 janv. 2012, 22:20:27
 */
package app.views;

import app.App;
import app.models.Lignevente;
import app.models.Vente;

/**
 *
 * @author Ghost
 */
public class PaiementDialog extends javax.swing.JDialog {
    
    public static String TYPE_NULL = "";
    
    private String type = TYPE_NULL;
    
    private Vente vente;

    /** Creates new form PaiementDialog */
    public PaiementDialog(Vente v) {
        super(App.getInstance().getMainFrame(), true);
        setLocationRelativeTo(null);
        
        vente = v;
        type = v.getType();
        initComponents();
        
        syncUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        paiementGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        btEnregistrer = new javax.swing.JButton();
        btAnnuler = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtMontant = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        optCB = new javax.swing.JRadioButton();
        optCheque = new javax.swing.JRadioButton();
        optEspece = new javax.swing.JRadioButton();
        panEspece = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtDonne = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtRendre = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btEnregistrer.setText("Enregistrer");
        btEnregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEnregistrerActionPerformed(evt);
            }
        });

        btAnnuler.setText("Annuler");
        btAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAnnulerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(127, Short.MAX_VALUE)
                .addComponent(btAnnuler)
                .addGap(18, 18, 18)
                .addComponent(btEnregistrer)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btEnregistrer)
                    .addComponent(btAnnuler))
                .addContainerGap())
        );

        jLabel1.setText("Montant:");

        txtMontant.setEditable(false);
        txtMontant.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtMontant.setText("6.66");

        jLabel2.setText("euros TTC");

        jLabel3.setText("Type de paiement:");

        paiementGroup.add(optCB);
        optCB.setText("Carte Bancaire");
        optCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optCBActionPerformed(evt);
            }
        });

        paiementGroup.add(optCheque);
        optCheque.setText("Chèque");
        optCheque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optChequeActionPerformed(evt);
            }
        });

        paiementGroup.add(optEspece);
        optEspece.setText("Espèces");
        optEspece.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optEspeceActionPerformed(evt);
            }
        });

        jLabel4.setText("Donné:");

        txtDonne.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtDonne.setText("0.00");
        txtDonne.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDonneKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDonneKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDonneKeyTyped(evt);
            }
        });

        jLabel5.setText("euros");

        jLabel6.setText("A rentre:");

        txtRendre.setEditable(false);
        txtRendre.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtRendre.setText("0.00");

        jLabel7.setText("euros");

        javax.swing.GroupLayout panEspeceLayout = new javax.swing.GroupLayout(panEspece);
        panEspece.setLayout(panEspeceLayout);
        panEspeceLayout.setHorizontalGroup(
            panEspeceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panEspeceLayout.createSequentialGroup()
                .addGroup(panEspeceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panEspeceLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtDonne, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addGroup(panEspeceLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(txtRendre, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)))
                .addContainerGap())
        );
        panEspeceLayout.setVerticalGroup(
            panEspeceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panEspeceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panEspeceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDonne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panEspeceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtRendre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optEspece)
                    .addComponent(optCheque)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(txtMontant, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addComponent(optCB))
                .addContainerGap())
            .addComponent(panEspece, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMontant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(optCB)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(optCheque)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(optEspece)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panEspece, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void optCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optCBActionPerformed
        type = Vente.TYPE_CB;
        syncUI();
    }//GEN-LAST:event_optCBActionPerformed

    private void optChequeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optChequeActionPerformed
        type = Vente.TYPE_CHEQUE;
        syncUI();
    }//GEN-LAST:event_optChequeActionPerformed

    private void optEspeceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optEspeceActionPerformed
        type = Vente.TYPE_ESPECES;
        syncUI();
    }//GEN-LAST:event_optEspeceActionPerformed

    private void txtDonneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonneKeyPressed
        syncUI();
    }//GEN-LAST:event_txtDonneKeyPressed

    private void btEnregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEnregistrerActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btEnregistrerActionPerformed

    private void btAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAnnulerActionPerformed
        type = TYPE_NULL;
        this.setVisible(false);
    }//GEN-LAST:event_btAnnulerActionPerformed

    private void txtDonneKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonneKeyReleased
        syncUI();
    }//GEN-LAST:event_txtDonneKeyReleased

    private void txtDonneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonneKeyTyped
        syncUI();
    }//GEN-LAST:event_txtDonneKeyTyped

    private void syncUI(){
        //panel visible que si on est en paiment en espèce
        panEspece.setVisible(Vente.TYPE_ESPECES.equals(type));
        
        //TODO afficher le montant de la vente
        if(vente.getTotal() > 0){
            txtMontant.setText(String.valueOf(vente.getTotal()));
        }
        
        if(Vente.TYPE_ESPECES.equals(type) && vente.getTotal() > 0){
            //calcul du total a rendre
            float aRendre = 0.f;
            float donne = -1;
            try{
                donne = Float.parseFloat(txtDonne.getText());
                aRendre = donne - vente.getTotal();
                if(aRendre < 0){
                    txtRendre.setText("Arnaque! Pas assez...");
                } else {
                    txtRendre.setText(String.valueOf(aRendre));
                }
            } catch(NumberFormatException nfe){
                txtRendre.setText("Raté!");
            }
            
        } 
        
        //redefinir la taille de la fenêtre
        pack();
    }
    
    public String showDialog() {
        this.setVisible(true);
        return type;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAnnuler;
    private javax.swing.JButton btEnregistrer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton optCB;
    private javax.swing.JRadioButton optCheque;
    private javax.swing.JRadioButton optEspece;
    private javax.swing.ButtonGroup paiementGroup;
    private javax.swing.JPanel panEspece;
    private javax.swing.JTextField txtDonne;
    private javax.swing.JTextField txtMontant;
    private javax.swing.JTextField txtRendre;
    // End of variables declaration//GEN-END:variables
}
